<?php


namespace App\Logic;

use App\Models\RealEstates;
use App\server\EsServer;
use App\server\RedisServer;
use Illuminate\Support\Facades\DB;

use App\server\RankServer;
use servers\wjy\AliPayStrategy;
use servers\wjy\PayStrategy;
use servers\wjy\WxPayStrategy;

/**
 * 楼盘逻辑层
 * Class AdminLogic
 * @package App\Logic
 * @ name:zf
 */
class realeEstateLogic{

    /**
     * Undocumented function
     * 列表
     * @param [type] $id
     * @return void
     */
    public static function realeEstateList(){
        $redis=RedisServer::createRedisObject();
        $redis->select(9);
        $realeEstateList= ['data'=>$redis->getAll()];
        if(empty($realeEstateList['data'])){
            $realeEstateList=RealEstates::list();
            self::realeEstateRedis();
        }
        return $realeEstateList;
    }
    /**
     * 楼盘信息存入ES
     */
    public static function realeEstateEsCreate($realeEstateList){
            foreach($realeEstateList as $v){
                (new EsServer())->EsAdd('reale_estate_index',$v);
            }
           return true;
    }
    /**
     * 楼盘信息ES搜索
     */
    public static function realeEstateEsIndex($name){
        if(!empty($name)){
            RankServer::rank($name);
            SeachLogic::SeachEsInsert($name);
        }
        $realeEstateData= (new EsServer())->searchEs('reale_estate_index',$name,'name');
        return $realeEstateData;
    }
     /**
     * 楼盘信息详情
     */
    public static function realeEstateDetail($id){
        $redis=RedisServer::createRedisObject();
        $redis->select(9);
        $realeEstateDetail=$redis->hGetone($id);
        if(empty($realeEstateDetail)){
            $realeEstateDetail=RealEstates::detail('id',$id);
            $data=$realeEstateDetail->toArray();
            $redis->hSetone($data);
        }
        return $realeEstateDetail;
    }
     /**
     * 楼盘信息编辑并存入ES
     */
    public static function realeEstateSave($realeEstateData,$request){
        $request->validate([
            'name' =>'required',
            'region'  =>'required',
            'type'=>'required'
        ]);
        if(empty($realeEstateData['id'])||!isset($realeEstateData['id'])){
           $user= RealEstates::create($realeEstateData);
           return $user;
        }else{
            return RealEstates::change('id',$realeEstateData['id'],$realeEstateData);
        }
    }
    /**
     *
     * 楼盘信息存入redis
     */
    public static function realeEstateRedis(){
        $realEstatedata= RealEstates::get()->toArray();
        $redis=RedisServer::createRedisObject();
        $redis->select(9);
        $redis->hSetAll($realEstatedata);
    }
    /**
     * 浏览
     */
    public static function floorRanking($reals_name,$way)
    {
        $redis= RedisServer::createRedisObject();
        $redis->select(13);
        $redis->zIncrBy('day',1,$reals_name);
        $redis->zIncrBy('week',1,$reals_name);
        switch ($way){
            case 0:
                return self::day();
            case 1:
                return self::week();
        }
    }

    /**
     * 查询日
     * @return mixed
     */
    function day(){
        $redis= RedisServer::createRedisObject();
        $redis->select(13);
        $day=$redis->zRevRange('day',0,-1);
        foreach ($day as $item =>$value){
            $arr['reals_name']=$item;
            $arr['Ranking_browse']=$value;
            $arr['status']=1;
            $arr['created_at']=date('Y-m-d H:i:s');
            $say[]=$arr;
        }
        return $say;
    }

    /**
     * 查询周
     * @return mixed
     */
    function week(){
        $redis= RedisServer::createRedisObject();
        $redis->select(13);
        $week=$redis->zRevRange('week',0,-1);
        foreach ($week as $item =>$value){
            $arr['reals_name']=$item;
            $arr['Ranking_browse']=$value;
            $arr['status']=1;
            $arr['created_at']=date('Y-m-d H:i:s');
            $say[]=$arr;
        }
        return $say;
    }
}
